﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Interfejsi;
using source;
using MySql.Data.MySqlClient;

namespace DAL
{
    partial class DAL
    {
        public class PedagogDAO : IDaoCrud<Pedagog>
        {
            #region IDaoCrud<Pedagog> Members
            protected MySqlCommand c;
            public long create(Pedagog entity)
            {
                try
                {
                    c = new MySqlCommand("insert into pedagog (korisnik_id, cv, pocetak_sluzbe) values (@KorisnikID, @CV, @PocetakSluzbe);", con);
                    c.Parameters.AddWithValue("@KorisnikID", entity.ID);
                    c.Parameters.AddWithValue("@CV", entity.CV);
                    c.Parameters.AddWithValue("@PocetakSluzbe", entity.PocetakSluzbe);
                    c.ExecuteNonQuery();
                    return entity.ID;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Pedagog> getAll()
            {
                try
                {
                    c = new MySqlCommand("select * from korisnik, pedagog where korisnik_id = id;", con);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Pedagog> pedagozi = new List<Pedagog>();
                    while (r.Read())
                    {
                        Pedagog pedagog = new Pedagog();
                        pedagog.ID = r.GetInt32("id");
                        pedagog.Username = r.GetString("username");
                        pedagog.Password = r.GetString("password");
                        pedagog.Tip = r.GetString("tip");
                        pedagog.Ime = r.GetString("ime");
                        pedagog.Prezime = r.GetString("prezime");
                        pedagog.JMBG = r.GetString("jmbg");
                        pedagog.DatumRodjenja = r.GetDateTime("datum_rodjenja");
                        pedagog.Adresa = r.GetString("adresa");
                        pedagog.Telefon = r.GetString("telefon");
                        pedagog.Email = r.GetString("email");
                        pedagog.Drzavljanstvo = r.GetString("drzavljanstvo");
                        pedagog.Slika = r.GetString("slika");
                        pedagog.CV = r.GetString("cv");
                        pedagog.PocetakSluzbe = r.GetDateTime("pocetak_sluzbe");
                        pedagozi.Add(pedagog);
                    }
                    r.Close();
                    c.Dispose();
                    return pedagozi;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Pedagog read(Pedagog entity)
            {
                try
                {
                    c = new MySqlCommand("select * from korisnik, pedagog where id = @ID; and korisnik_id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    MySqlDataReader r = c.ExecuteReader();
                    Pedagog pedagog = new Pedagog();
                    if (r.Read())
                    {
                        pedagog.ID = r.GetInt32("id");
                        pedagog.Username = r.GetString("username");
                        pedagog.Password = r.GetString("password");
                        pedagog.Tip = r.GetString("tip");
                        pedagog.Ime = r.GetString("ime");
                        pedagog.Prezime = r.GetString("prezime");
                        pedagog.JMBG = r.GetString("jmbg");
                        pedagog.DatumRodjenja = r.GetDateTime("datum_rodjenja");
                        pedagog.Adresa = r.GetString("adresa");
                        pedagog.Telefon = r.GetString("telefon");
                        pedagog.Email = r.GetString("email");
                        pedagog.Drzavljanstvo = r.GetString("drzavljanstvo");
                        pedagog.Slika = r.GetString("slika");
                        pedagog.CV = r.GetString("cv");
                        pedagog.PocetakSluzbe = r.GetDateTime("pocetak_sluzbe");
                    }
                    r.Close();
                    c.Dispose();
                    return pedagog;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Pedagog update(Pedagog entity)
            {
                try
                {
                    c = new MySqlCommand("UPDATE pedagog SET cv = @CV, pocetak_sluzbe = @PocetakSluzbe where korisnik_id = @KorisnikID;", con);
                    c.Parameters.AddWithValue("@CV", entity.CV);
                    c.Parameters.AddWithValue("@PocetakSluzbe", entity.PocetakSluzbe);
                    c.Parameters.AddWithValue("@KorisnikID", entity.ID);
                    c.ExecuteNonQuery();
                    return entity;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public void delete(Pedagog entity)
            {
                try
                {
                    c = new MySqlCommand("delete from pedagog where korisnik_id = @KorisnikID;", con);
                    c.Parameters.AddWithValue("@KorisnikID", entity.ID);
                    c.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Pedagog getById(int id)
            {
                try
                {
                    c = new MySqlCommand("select * from korisnik, pedagog where id = @ID and korisnik_id = id;", con);
                    c.Parameters.AddWithValue("@ID", id);
                    MySqlDataReader r = c.ExecuteReader();
                    Pedagog pedagog = new Pedagog();
                    if (r.Read())
                    {
                        pedagog.ID = r.GetInt32("id");
                        pedagog.Username = r.GetString("username");
                        pedagog.Password = r.GetString("password");
                        pedagog.Tip = r.GetString("tip");
                        pedagog.Ime = r.GetString("ime");
                        pedagog.Prezime = r.GetString("prezime");
                        pedagog.JMBG = r.GetString("jmbg");
                        pedagog.DatumRodjenja = r.GetDateTime("datum_rodjenja");
                        pedagog.Adresa = r.GetString("adresa");
                        pedagog.Telefon = r.GetString("telefon");
                        pedagog.Email = r.GetString("email");
                        pedagog.Drzavljanstvo = r.GetString("drzavljanstvo");
                        pedagog.Slika = r.GetString("slika");
                        pedagog.CV = r.GetString("cv");
                        pedagog.PocetakSluzbe = r.GetDateTime("pocetak_sluzbe");
                    }
                    r.Close();
                    c.Dispose();
                    return pedagog;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Pedagog> getByExample(string name, string value)
            {
                try
                {
                    c = new MySqlCommand("select * from korisnik, pedagog where @Kolona = @Vrijednost and korisnik_id = id;", con);
                    c.Parameters.AddWithValue("@Kolona", name);
                    c.Parameters.AddWithValue("@Vrijednost", value);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Pedagog> pedagozi = new List<Pedagog>();
                    while (r.Read())
                    {
                        Pedagog pedagog = new Pedagog();
                        pedagog.ID = r.GetInt32("id");
                        pedagog.Username = r.GetString("username");
                        pedagog.Password = r.GetString("password");
                        pedagog.Tip = r.GetString("tip");
                        pedagog.Ime = r.GetString("ime");
                        pedagog.Prezime = r.GetString("prezime");
                        pedagog.JMBG = r.GetString("jmbg");
                        pedagog.DatumRodjenja = r.GetDateTime("datum_rodjenja");
                        pedagog.Adresa = r.GetString("adresa");
                        pedagog.Telefon = r.GetString("telefon");
                        pedagog.Email = r.GetString("email");
                        pedagog.Drzavljanstvo = r.GetString("drzavljanstvo");
                        pedagog.Slika = r.GetString("slika");
                        pedagog.CV = r.GetString("cv");
                        pedagog.PocetakSluzbe = r.GetDateTime("pocetak_sluzbe");
                        pedagozi.Add(pedagog);
                    }
                    r.Close();
                    c.Dispose();
                    return pedagozi;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            #endregion
        }
    }
}
